package com.fau.cm.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallTaskStatistics;
import com.fau.cm.entity.MedicalGroup;
import com.fau.cm.entity.MedicalCallPerforAnalysis;
import com.fau.cm.entity.MedicalPlatform;
import com.fau.cm.service.IperforAnalysisManagerService;
import com.fau.cm.util.DateUtil;
import com.fau.cm.util.Page;
import com.fau.cm.util.ParamUtil;
import com.fau.cm.util.SystemConfig;

@Service
@Transactional(readOnly = true)
public class perforAnalysisService extends BaseService implements
		IperforAnalysisManagerService {

	@SuppressWarnings("unchecked")
	@Override
	public Page findStaffperforAnalysis(MedicalCallPerforAnalysis perforAnalysis, MedicalAdmin admin, Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallPerforAnalysis a where 1=1 and a.hospitalId=? ");
		StringBuilder countSql = new StringBuilder("select count(distinct a.adminId) from MedicalCallPerforAnalysis a where 1=1 and a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		Date startTime = null;
		Date endTime = null;
		if(StringUtils.isBlank(perforAnalysis.getEndTime())) {
			endTime = new Date();
		} else {
			endTime = DateUtil.getDate(perforAnalysis.getEndTime());
		}
		
		if(StringUtils.isNotBlank(perforAnalysis.getStartTime())) {
			startTime = DateUtil.getDate(perforAnalysis.getStartTime());
			statusSql.append("and a.ordersTime between ? and ? ");
			countSql.append("and a.ordersTime between ? and ? ");
			objectList.add(startTime);
			objectList.add(endTime);
		}
		// 只输入结束时间
		if (StringUtils.isBlank(perforAnalysis.getStartTime()) && StringUtils.isNotBlank(perforAnalysis.getEndTime())) {
			statusSql.append("and a.ordersTime < ? ");
			countSql.append("and a.ordersTime < ? ");
			objectList.add(endTime);
		}
		
		statusSql.append("group by a.adminId ");
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallPerforAnalysis> perforAnalysisList = new ArrayList<MedicalCallPerforAnalysis>();
		
		for(Object obj : page.getResult()) {
			perforAnalysis = (MedicalCallPerforAnalysis) obj;
			// 查用户信息
			MedicalAdmin staff = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, perforAnalysis.getAdminId());
			perforAnalysis.setAdminJobNum(staff.getUsername());
			perforAnalysis.setAdminName(staff.getName());
			if(!SystemConfig.SUPER_ADMIN.equals(staff.getIsSuper())) {
				perforAnalysis.setGroupName(staff.getGroup().getName());
			}
			
			// 查去重后的呼入数
			int incomingCount = this.iBaseDao.getIntCount("select count(distinct a.callerNum) from MedicalCallCaller a where a.answerSeat=? ", staff.getId());
			perforAnalysis.setIncomingCount(incomingCount);
			
			// 查询呼出数
			int outgoingCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallExhale a where a.operator=? ", staff.getId());
			perforAnalysis.setOutgoingCount(outgoingCount);
			
			// 查询订单数
			int ordersCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallPerforAnalysis a where a.adminId=? ", staff.getId());
			perforAnalysis.setOrdersCount(ordersCount);
			
			// 订单货款总额、运费
			List<Object> objList = this.iBaseDao.getBySql("select sum(a.commodityMoney), sum(a.freight), sum(a.tMoney), sum(a.productQuantity) from MedicalCallPerforAnalysis a where a.adminId=? ", null, staff.getId());
			for(Object obj1 : objList) {
				Object[] money1 = (Object[]) obj1;
				if(null != money1[0] && null != money1[1] && null != money1[2] && null != money1[3]) {
					perforAnalysis.settCommodityMoney(new BigDecimal(money1[0].toString()));
					perforAnalysis.settFreight(new BigDecimal(money1[1].toString()));
					perforAnalysis.settTMoney(new BigDecimal(money1[2].toString()));
					perforAnalysis.settProductQuantity(Integer.parseInt(money1[3].toString()));
				}
			}
			perforAnalysisList.add(perforAnalysis);
		}
		
		final String[] sortField = (String[]) map.get("sortField");
		final String[] sortOrder = (String[]) map.get("sortOrder");
		Collections.sort(perforAnalysisList, new Comparator<MedicalCallPerforAnalysis>() {
			@Override
			public int compare(MedicalCallPerforAnalysis o1, MedicalCallPerforAnalysis o2) {
				if(sortField[0].equals("ordersCount")) {
					if(sortOrder[0].equals("asc")) {
						return o1.getOrdersCount().compareTo(o2.getOrdersCount());
					} else {
						return o2.getOrdersCount().compareTo(o1.getOrdersCount());
					}
				} else if(sortField[0].equals("tTMoney")) {
					if(sortOrder[0].equals("asc")) {
						return o1.gettMoney().compareTo(o2.gettMoney());
					} else {
						return o2.gettMoney().compareTo(o1.gettMoney());
					}
				} else if(sortField[0].equals("avgMoney")) {
					if(sortOrder[0].equals("asc")) {
						return o1.getAvgMoney().compareTo(o2.getAvgMoney());
					} else {
						return o2.getAvgMoney().compareTo(o1.getAvgMoney());
					}
				} else if(sortField[0].equals("tProductQuantity")) {
					if(sortOrder[0].equals("asc")) {
						return o1.gettProductQuantity().compareTo(o2.gettProductQuantity());
					} else {
						return o2.gettProductQuantity().compareTo(o1.gettProductQuantity());
					}
				} else {
					return 0;
				}
			}
		});
		
		page.setResult(perforAnalysisList);
		return page;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportStaffperforAnalysis(MedicalAdmin admin,MedicalCallPerforAnalysis perforAnalysis) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallPerforAnalysis a where a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		Date endTime = null;
		if(null == perforAnalysis.getEtime()) {
			endTime = new Date();
		} else {
			endTime = perforAnalysis.getEtime();
		}
		
		if(null != perforAnalysis.getStime()) {
			statusSql.append("and a.ordersTime between ? and ? ");
			objectList.add(perforAnalysis.getStime());
			objectList.add(endTime);
		}
		// 只输入结束时间
		if (null == perforAnalysis.getStime() && null != perforAnalysis.getEtime()) {
			statusSql.append("and a.ordersTime < ? ");
			objectList.add(perforAnalysis.getEtime());
		}
		
		statusSql.append("group by a.adminId ");
		
		List<MedicalCallPerforAnalysis> list = this.iBaseDao.getBySql(statusSql.toString(), null, objectList.toArray());
		
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.STAFF_PERFORMANCE_STATISTICS_FILE);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		
		int size = list.size();
		for(int i = 0;i < size;i++) {
			Row currentRow = sheet.createRow(i+1);
			// 查用户信息
			MedicalAdmin staff = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, list.get(i).getAdminId());
			if(null != staff){
				currentRow.createCell(0).setCellValue(staff.getUsername());
				currentRow.createCell(1).setCellValue(staff.getName());
				currentRow.createCell(2).setCellValue(staff.getGroup().getName());
			}
			
			// 查去重后的呼入数
			int incomingCount = this.iBaseDao.getIntCount("select count(distinct a.callerNum) from MedicalCallCaller a where a.answerSeat=? ", staff.getId());
			currentRow.createCell(3).setCellValue(incomingCount);
			
			// 查询呼出数
			int outgoingCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallExhale a where a.operator=? ", staff.getId());
			perforAnalysis.setOutgoingCount(outgoingCount);
			currentRow.createCell(4).setCellValue(outgoingCount);
			
			// 查询订单数
			int ordersCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallPerforAnalysis a where a.adminId=? ", staff.getId());
			perforAnalysis.setOrdersCount(ordersCount);
			currentRow.createCell(5).setCellValue(ordersCount);
			
			// 订单货款总额、运费
			List<Object> objList = this.iBaseDao.getBySql("select sum(a.commodityMoney), sum(a.freight), sum(a.tMoney), sum(a.productQuantity) from MedicalCallPerforAnalysis a where a.adminId=? ", null, staff.getId());
			for(Object obj1 : objList) {
				Object[] money1 = (Object[]) obj1;
				if(null != money1[0] && null != money1[1] && null != money1[2] && null != money1[3]) {
					currentRow.createCell(6).setCellValue(money1[0].toString());
					currentRow.createCell(7).setCellValue(money1[1].toString());
					currentRow.createCell(8).setCellValue(money1[2].toString());
					currentRow.createCell(9).setCellValue(money1[3].toString());
				}
			}
			currentRow.createCell(10).setCellValue(i+1);
		}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page findGroupPerforAnalysis(MedicalCallPerforAnalysis perforAnalysis, MedicalAdmin admin, Map<String, Object> map) {

		StringBuilder statusSql = new StringBuilder("from MedicalCallPerforAnalysis a where 1=1 and a.hospitalId=? ");
		StringBuilder countSql = new StringBuilder("select count(distinct a.groupId) from MedicalCallPerforAnalysis a where 1=1 and a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		Date startTime = null;
		Date endTime = null;
		if(StringUtils.isBlank(perforAnalysis.getEndTime())) {
			endTime = new Date();
		} else {
			endTime = DateUtil.getDate(perforAnalysis.getEndTime());
		}
		
		if(StringUtils.isNotBlank(perforAnalysis.getStartTime())) {
			startTime = DateUtil.getDate(perforAnalysis.getStartTime());
			statusSql.append("and a.ordersTime between ? and ? ");
			countSql.append("and a.ordersTime between ? and ? ");
			objectList.add(startTime);
			objectList.add(endTime);
		}
		// 只输入结束时间
		if (StringUtils.isBlank(perforAnalysis.getStartTime()) && StringUtils.isNotBlank(perforAnalysis.getEndTime())) {
			statusSql.append("and a.ordersTime < ? ");
			countSql.append("and a.ordersTime < ? ");
			objectList.add(endTime);
		}
		
		statusSql.append("group by a.groupId ");
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallPerforAnalysis> perforAnalysisList = new ArrayList<MedicalCallPerforAnalysis>();
		for(Object obj : page.getResult()) {
			perforAnalysis = (MedicalCallPerforAnalysis) obj;
			
			if(StringUtils.isBlank(perforAnalysis.getGroupId())) continue;

			// 查小组信息
			MedicalGroup group = (MedicalGroup) this.iBaseDao.getObject(MedicalGroup.class, perforAnalysis.getGroupId());
			perforAnalysis.setGroupName(group.getName());
			// 查组中人数
			List<String> adminIdList = this.iBaseDao.getBySql("select distinct(a.adminId) from MedicalCallPerforAnalysis a where a.groupId=? ", null, group.getId());
			perforAnalysis.setUserCount(adminIdList.size());
			int incomingCount = 0;	// 查去重后的呼入数
			int outgoingCount = 0;	// 查询呼出数(去重)
			for(String adminId : adminIdList) {
				incomingCount += this.iBaseDao.getIntCount("select count(distinct a.callerNum) from MedicalCallCaller a where a.answerSeat=? ", adminId);
				outgoingCount += this.iBaseDao.getIntCount("select count(*) from MedicalCallExhale a where a.operator=? ", adminId);
			}
			perforAnalysis.setIncomingCount(incomingCount);
			perforAnalysis.setOutgoingCount(outgoingCount);
			// 查询订单数
			int ordersCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallPerforAnalysis a where a.groupId=? ", group.getId());
			perforAnalysis.setOrdersCount(ordersCount);
			
			// 订单货款总额、运费
			List<Object> objList = this.iBaseDao.getBySql("select sum(a.tMoney), sum(a.productQuantity) from MedicalCallPerforAnalysis a where a.groupId=? ", null, group.getId());
			for(Object obj1 : objList) {
				Object[] money1 = (Object[]) obj1;
				if(null != money1[0] && null != money1[1]) {
					perforAnalysis.settTMoney(new BigDecimal(money1[0].toString()));
					perforAnalysis.settProductQuantity(Integer.parseInt(money1[1].toString()));
					BigDecimal avgMoney = new BigDecimal(money1[0].toString()).divide(new BigDecimal(adminIdList.size()), 2);
					perforAnalysis.setAvgMoney(avgMoney);
				}
			}
			perforAnalysisList.add(perforAnalysis);
		}
		
		final String[] sortField = (String[]) map.get("sortField");
		final String[] sortOrder = (String[]) map.get("sortOrder");
		Collections.sort(perforAnalysisList, new Comparator<MedicalCallPerforAnalysis>() {
			@Override
			public int compare(MedicalCallPerforAnalysis o1, MedicalCallPerforAnalysis o2) {
				if(sortField[0].equals("ordersCount")) {
					if(sortOrder[0].equals("asc")) {
						return o1.getOrdersCount().compareTo(o2.getOrdersCount());
					} else {
						return o2.getOrdersCount().compareTo(o1.getOrdersCount());
					}
				} else if(sortField[0].equals("tTMoney")) {
					if(sortOrder[0].equals("asc")) {
						return o1.gettMoney().compareTo(o2.gettMoney());
					} else {
						return o2.gettMoney().compareTo(o1.gettMoney());
					}
				} else if(sortField[0].equals("avgMoney")) {
					if(sortOrder[0].equals("asc")) {
						return o1.getAvgMoney().compareTo(o2.getAvgMoney());
					} else {
						return o2.getAvgMoney().compareTo(o1.getAvgMoney());
					}
				} else if(sortField[0].equals("tProductQuantity")) {
					if(sortOrder[0].equals("asc")) {
						return o1.gettProductQuantity().compareTo(o2.gettProductQuantity());
					} else {
						return o2.gettProductQuantity().compareTo(o1.gettProductQuantity());
					}
				} else {
					return 0;
				}
			}
		});
		page.setResult(perforAnalysisList);
		return page;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportGroupPerforAnalysis(MedicalAdmin admin,MedicalCallPerforAnalysis perforAnalysis) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallPerforAnalysis a where a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		Date endTime = null;
		if(null == perforAnalysis.getEtime()) {
			endTime = new Date();
		} else {
			endTime = perforAnalysis.getEtime();
		}
		
		if(null != perforAnalysis.getStime()) {
			statusSql.append("and a.ordersTime between ? and ? ");
			objectList.add(perforAnalysis.getStime());
			objectList.add(endTime);
		}
		// 只输入结束时间
		if (null == perforAnalysis.getStime() && null != perforAnalysis.getEtime()) {
			statusSql.append("and a.ordersTime < ? ");
			objectList.add(perforAnalysis.getEtime());
		}
		
		statusSql.append("group by a.groupId ");
		
		List<MedicalCallPerforAnalysis> list = this.iBaseDao.getBySql(statusSql.toString(), null, objectList.toArray());
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.STAFF_GROUP_STATISTICS_FILE);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		
		int size = list.size();
		for(int i = 0;i < size;i++) {
			Row currentRow = sheet.createRow(i+1);
			// 查小组信息
			MedicalGroup group = (MedicalGroup) this.iBaseDao.getObject(MedicalGroup.class, list.get(i).getGroupId());
			if(null != group){
				currentRow.createCell(0).setCellValue(group.getName());
				
				// 查组中人数
				List<String> adminIdList = this.iBaseDao.getBySql("select distinct(a.adminId) from MedicalCallPerforAnalysis a where a.groupId=? ", null, group.getId());
				currentRow.createCell(1).setCellValue(adminIdList.size());
				
				int incomingCount = 0;	// 查去重后的呼入数
				int outgoingCount = 0;	// 查询呼出数(去重)
				for(String adminId : adminIdList) {
					incomingCount += this.iBaseDao.getIntCount("select count(distinct a.callerNum) from MedicalCallCaller a where a.answerSeat=? ", adminId);
					outgoingCount += this.iBaseDao.getIntCount("select count(*) from MedicalCallExhale a where a.operator=? ", adminId);
				}
				currentRow.createCell(2).setCellValue(incomingCount);
				currentRow.createCell(3).setCellValue(outgoingCount);
				
				// 查询订单数
				int ordersCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallPerforAnalysis a where a.groupId=? ", group.getId());
				currentRow.createCell(4).setCellValue(ordersCount);
				
				// 订单货款总额、运费
				List<Object> objList = this.iBaseDao.getBySql("select sum(a.tMoney), sum(a.productQuantity) from MedicalCallPerforAnalysis a where a.groupId=? ", null, group.getId());
				for(Object obj1 : objList) {
					Object[] money1 = (Object[]) obj1;
					if(null != money1[0] && null != money1[1]) {
						currentRow.createCell(5).setCellValue(money1[0].toString());
						currentRow.createCell(7).setCellValue(money1[1].toString());
						BigDecimal avgMoney = new BigDecimal(money1[0].toString()).divide(new BigDecimal(adminIdList.size()), 2);
						currentRow.createCell(6).setCellValue(avgMoney.toString());
					}
				}
				currentRow.createCell(8).setCellValue(i+1);
			}
			
		}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page findCenterPerforAnalysis(MedicalCallPerforAnalysis perforAnalysis, MedicalAdmin admin, Map<String, Object> map) {

		StringBuilder statusSql = new StringBuilder("from MedicalCallPerforAnalysis a where 1=1 and a.hospitalId=? ");
		StringBuilder countSql = new StringBuilder("select count(distinct a.centerId) from MedicalCallPerforAnalysis a where 1=1 and a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		Date startTime = null;
		Date endTime = null;
		if(StringUtils.isBlank(perforAnalysis.getEndTime())) {
			endTime = new Date();
		} else {
			endTime = DateUtil.getDate(perforAnalysis.getEndTime());
		}
		
		if(StringUtils.isNotBlank(perforAnalysis.getStartTime())) {
			startTime = DateUtil.getDate(perforAnalysis.getStartTime());
			statusSql.append("and a.ordersTime between ? and ? ");
			countSql.append("and a.ordersTime between ? and ? ");
			objectList.add(startTime);
			objectList.add(endTime);
		}
		// 只输入结束时间
		if (StringUtils.isBlank(perforAnalysis.getStartTime()) && StringUtils.isNotBlank(perforAnalysis.getEndTime())) {
			statusSql.append("and a.ordersTime < ? ");
			countSql.append("and a.ordersTime < ? ");
			objectList.add(endTime);
		}
		
		statusSql.append("group by a.centerId ");
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallPerforAnalysis> perforAnalysisList = new ArrayList<MedicalCallPerforAnalysis>();
		
		for(Object obj : page.getResult()) {
			perforAnalysis = (MedicalCallPerforAnalysis) obj;
			
			if(StringUtils.isBlank(perforAnalysis.getCenterId())) continue;
			// 查中心
			MedicalPlatform center = (MedicalPlatform) this.iBaseDao.getObject(MedicalPlatform.class, perforAnalysis.getCenterId());
			perforAnalysis.setCenterName(center.getName());
			
			// 查人数
			List<String> adminIdList = this.iBaseDao.getBySql("select distinct(a.adminId) from MedicalCallPerforAnalysis a where a.centerId=? ", null, center.getId());
			perforAnalysis.setUserCount(adminIdList.size());
			
			int incomingCount = 0;	// 查去重后的呼入数
			int outgoingCount = 0; 	// 查询呼出数
			for(String adminId : adminIdList) {
				incomingCount += this.iBaseDao.getIntCount("select count(distinct a.callerNum) from MedicalCallCaller a where a.answerSeat=? ", adminId);
				outgoingCount += this.iBaseDao.getIntCount("select count(*) from MedicalCallExhale a where a.operator=? ", adminId);
			}
			perforAnalysis.setIncomingCount(incomingCount);
			perforAnalysis.setOutgoingCount(outgoingCount);
			
			// 查询订单数
			int ordersCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallPerforAnalysis a where a.centerId=?", center.getId());
			perforAnalysis.setOrdersCount(ordersCount);
			
			// 订单货款总额、运费
			List<Object> objList = this.iBaseDao.getBySql("select sum(a.tMoney), sum(a.productQuantity) from MedicalCallPerforAnalysis a where a.centerId=? ", null, center.getId());
			for(Object obj1 : objList) {
				Object[] money1 = (Object[]) obj1;
				if(null != money1[0] && null != money1[1]) {
					perforAnalysis.settTMoney(new BigDecimal(money1[0].toString()));
					perforAnalysis.settProductQuantity(Integer.parseInt(money1[1].toString()));
					BigDecimal avgMoney = new BigDecimal(money1[0].toString()).divide(new BigDecimal(adminIdList.size()), 2);
					perforAnalysis.setAvgMoney(avgMoney);
				}
			}
			perforAnalysisList.add(perforAnalysis);
		}

		final String[] sortField = (String[]) map.get("sortField");
		final String[] sortOrder = (String[]) map.get("sortOrder");
		Collections.sort(perforAnalysisList, new Comparator<MedicalCallPerforAnalysis>() {
			@Override
			public int compare(MedicalCallPerforAnalysis o1, MedicalCallPerforAnalysis o2) {
				if(sortField[0].equals("ordersCount")) {
					if(sortOrder[0].equals("asc")) {
						return o1.getOrdersCount().compareTo(o2.getOrdersCount());
					} else {
						return o2.getOrdersCount().compareTo(o1.getOrdersCount());
					}
				} else if(sortField[0].equals("tTMoney")) {
					if(sortOrder[0].equals("asc")) {
						return o1.gettMoney().compareTo(o2.gettMoney());
					} else {
						return o2.gettMoney().compareTo(o1.gettMoney());
					}
				} else if(sortField[0].equals("avgMoney")) {
					if(sortOrder[0].equals("asc")) {
						return o1.getAvgMoney().compareTo(o2.getAvgMoney());
					} else {
						return o2.getAvgMoney().compareTo(o1.getAvgMoney());
					}
				} else if(sortField[0].equals("tProductQuantity")) {
					if(sortOrder[0].equals("asc")) {
						return o1.gettProductQuantity().compareTo(o2.gettProductQuantity());
					} else {
						return o2.gettProductQuantity().compareTo(o1.gettProductQuantity());
					}
				} else {
					return 0;
				}
			}
		});
		page.setResult(perforAnalysisList);
		return page;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportCenterPerforAnalysis(MedicalAdmin admin,MedicalCallPerforAnalysis perforAnalysis) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallPerforAnalysis a where 1=1 and a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		Date endTime = null;
		if(null == perforAnalysis.getEtime()) {
			endTime = new Date();
		} else {
			endTime = perforAnalysis.getEtime();
		}
		
		if(null != perforAnalysis.getStime()) {
			statusSql.append("and a.createTime between ? and ? ");
			objectList.add(perforAnalysis.getStime());
			objectList.add(endTime);
		}
		// 只输入结束时间
		if (null == perforAnalysis.getStime() && null != perforAnalysis.getEtime()) {
			statusSql.append("and a.createTime < ? ");
			objectList.add(perforAnalysis.getEtime());
		}
		
		statusSql.append("group by a.centerId ");
		
		List<MedicalCallPerforAnalysis> list = this.iBaseDao.getBySql(statusSql.toString(), null, objectList.toArray());
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.STAFF_CENTER_STATISTICS_FILE);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		
		int size = list.size();
		for(int i = 0;i < size;i++) {
			Row currentRow = sheet.createRow(i+1);
			// 查中心
			MedicalPlatform center = (MedicalPlatform) this.iBaseDao.getObject(MedicalPlatform.class, list.get(i).getCenterId());
			if(null != center){
				currentRow.createCell(0).setCellValue(center.getName());
				
				// 查人数
				List<String> adminIdList = this.iBaseDao.getBySql("select distinct(a.adminId) from MedicalCallPerforAnalysis a where a.centerId=? ", null, center.getId());
				currentRow.createCell(1).setCellValue(adminIdList.size());
				
				int incomingCount = 0;	// 查去重后的呼入数
				int outgoingCount = 0; 	// 查询呼出数
				for(String adminId : adminIdList) {
					incomingCount += this.iBaseDao.getIntCount("select count(distinct a.callerNum) from MedicalCallCaller a where a.answerSeat=? ", adminId);
					outgoingCount += this.iBaseDao.getIntCount("select count(*) from MedicalCallExhale a where a.operator=? ", adminId);
				}
				currentRow.createCell(2).setCellValue(incomingCount);
				currentRow.createCell(3).setCellValue(outgoingCount);
				
				// 查询订单数
				int ordersCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallPerforAnalysis a where a.centerId=?", center.getId());
				currentRow.createCell(4).setCellValue(ordersCount);
				
				// 订单货款总额、运费
				List<Object> objList = this.iBaseDao.getBySql("select sum(a.tMoney), sum(a.productQuantity) from MedicalCallPerforAnalysis a where a.centerId=? ", null, center.getId());
				for(Object obj1 : objList) {
					Object[] money1 = (Object[]) obj1;
					if(null != money1[0] && null != money1[1]) {
						currentRow.createCell(5).setCellValue(money1[0].toString());
						currentRow.createCell(7).setCellValue(money1[1].toString());
						BigDecimal avgMoney = new BigDecimal(money1[0].toString()).divide(new BigDecimal(adminIdList.size()), 2);
						currentRow.createCell(6).setCellValue(avgMoney.toString());
					}
				}
				currentRow.createCell(8).setCellValue(i+1);
			}
		}
		return workbook;
	}

	@Override
	public Page findStaffTaskStatistics(MedicalCallTaskStatistics taskStatistics, MedicalAdmin admin, Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallTaskStatistics a where 1=1 and a.hospitalId=? ");
		StringBuilder countSql = new StringBuilder("select count(distinct a.handleUserId) from MedicalCallTaskStatistics a where 1=1 and a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		Date startTime = null;
		Date endTime = null;
		if(StringUtils.isBlank(taskStatistics.getEndTime())) {
			endTime = new Date();
		} else {
			endTime = DateUtil.getDate(taskStatistics.getEndTime());
		}
		
		if(StringUtils.isNotBlank(taskStatistics.getStartTime())) {
			startTime = DateUtil.getDate(taskStatistics.getStartTime());
			statusSql.append("and a.taskTime between ? and ? ");
			countSql.append("and a.taskTime between ? and ? ");
			objectList.add(startTime);
			objectList.add(endTime);
		}
		// 只输入结束时间
		if (StringUtils.isBlank(taskStatistics.getStartTime()) && StringUtils.isNotBlank(taskStatistics.getEndTime())) {
			statusSql.append("and a.taskTime < ? ");
			countSql.append("and a.taskTime < ? ");
			objectList.add(endTime);
		}
		
		statusSql.append("group by a.handleUserId ");
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallTaskStatistics> taskStatisticsList = new ArrayList<MedicalCallTaskStatistics>();
		for(Object obj : page.getResult()) {
			taskStatistics = (MedicalCallTaskStatistics) obj;
			MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, taskStatistics.getHandleUserId());
			taskStatistics.setHandUserName(handleUser.getName());
			// 查未处理任务
			int untreatedCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallTaskStatistics a where 1=1 and a.handleUserId=? and a.taskType=? ", taskStatistics.getHandleUserId(), SystemConfig.CALL_TASK_UNTREATED);
			taskStatistics.setUntreatedCount(untreatedCount);
			// 查待处理任务数
			int pendingCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallTaskStatistics a where 1=1 and a.handleUserId=? and a.taskType=? ", taskStatistics.getHandleUserId(), SystemConfig.CALL_TASK_PENDING);
			taskStatistics.setPendingCount(pendingCount);
			// 查已结束任务数
			int endCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallTaskStatistics a where 1=1 and a.handleUserId=? and a.taskType=? ", taskStatistics.getHandleUserId(), SystemConfig.CALL_TASK_END);
			taskStatistics.setEndCount(endCount);
			
			int totalCount = untreatedCount + pendingCount + endCount;
			taskStatistics.setTotalCount(totalCount);
			taskStatisticsList.add(taskStatistics);
		}
		page.setResult(taskStatisticsList);
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportStaffTaskStatistics(MedicalAdmin admin, MedicalCallTaskStatistics taskStatistics) throws Exception {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallTaskStatistics a where a.hospitalId=? ");
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		Date endTime = null;
		if(null == taskStatistics.getEtime()){
			endTime = new Date();
		}else{
			endTime = taskStatistics.getEtime();
		}
		
		if(null != taskStatistics.getStime()){
			statusSql.append("and a.taskTime between ? and ? ");
			objectList.add(taskStatistics.getStime());
			objectList.add(endTime);
		}
		
		// 只输入结束时间
		if((null == taskStatistics.getStime()) && (null != taskStatistics.getEtime())){
			statusSql.append("and a.taskTime < ? ");
			objectList.add(taskStatistics.getEtime());
		}
		
		statusSql.append("group by a.handleUserId ");
		
		List<MedicalCallTaskStatistics> list = this.iBaseDao.getBySql(statusSql.toString(), null, objectList.toArray());
		
		
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.STAFF_TASK_STATISTICS_FILE);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		// 遍历
		int size = list.size();
		for (int i = 0;i < size;i++) {
			Row currentRow = sheet.createRow(i+1);
			
			MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, list.get(i).getHandleUserId());
			
			if(StringUtils.isNotBlank(handleUser.getName())){
				currentRow.createCell(0).setCellValue(handleUser.getName());
			}
			
			// 查未处理任务
			int untreatedCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallTaskStatistics a where 1=1 and a.handleUserId=? and a.taskType=? ", list.get(i).getHandleUserId(), SystemConfig.CALL_TASK_UNTREATED);
			// 查待处理任务数
			int pendingCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallTaskStatistics a where 1=1 and a.handleUserId=? and a.taskType=? ", list.get(i).getHandleUserId(), SystemConfig.CALL_TASK_PENDING);
			// 查已结束任务数
			int endCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallTaskStatistics a where 1=1 and a.handleUserId=? and a.taskType=? ", list.get(i).getHandleUserId(), SystemConfig.CALL_TASK_END);
			
			int totalCount = untreatedCount + pendingCount + endCount;
			
			currentRow.createCell(1).setCellValue(totalCount);
			currentRow.createCell(2).setCellValue(untreatedCount);
			currentRow.createCell(3).setCellValue(pendingCount);
			currentRow.createCell(4).setCellValue(endCount);
			
		}
		return workbook;
	}

	
}
